//
//  UKTableViewProxy.m
//  UKListManager
//
//  Created by ET|冰琳 on 16/11/3.
//  Copyright © 2016年 UK. All rights reserved.
//

#import "UKTableViewProxy.h"
#import "CellBinder.h"
#import "UITableView+FDTemplateLayoutCell.h"

@interface UKTableViewProxy ()

@property (nonatomic, copy) NSString *identifier;
@property (nonatomic, assign) BOOL  cacheHeight;
@property (nonatomic, assign) id<UKListDataSource>  dataSource;
@property (nonatomic, copy) ListCellConfiguration cellConfiguration;


/**
 called by - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 */
@property (nonatomic, copy  , nullable) ListItemSelect selectItem;

@end

@implementation UKTableViewProxy

- (instancetype)initWithIdentifier:(NSString *)identifier
                       cacheHeight:(BOOL)cache
                        dataSource:(id<UKListDataSource>)dataSource
                        cellConfig:(nullable ListCellConfiguration)cellConfig{
    if (self = [super init]) {
        self.identifier = identifier;
        self.cacheHeight = cache;
        self.dataSource = dataSource;
        self.cellConfiguration = cellConfig;
    }
    return self;
}

- (void)setSelectItemAction:(ListItemSelect)selectItemAction{
    self.selectItem = selectItemAction;
}

#pragma mark - UITableViewDataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *identifier = [self identifierForIndexPath:indexPath];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: identifier];
    [self _configurationCell:cell atIndexPath:indexPath];
    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [self.dataSource sectionCount];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.dataSource rowCountAtSection:section];
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    // CacheHeight
    if (self.cacheHeight) {
        
        NSString *identifier = [self identifierForIndexPath:indexPath];
        [tableView fd_heightForCellWithIdentifier:identifier configuration:^(id cell) {
            [self _configurationCell:cell atIndexPath:indexPath];
        }];
    }
    
    if ([self.delegateForward conformsToProtocol:@protocol(UITableViewDelegate)]) {
        id<UITableViewDelegate> delegate = self.delegateForward;
        if ([delegate respondsToSelector:@selector(tableView:heightForRowAtIndexPath:)]) {
            return [delegate tableView:tableView heightForRowAtIndexPath:indexPath];
        }
    }
    return tableView.rowHeight;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.selectItem) {
        self.selectItem([self.dataSource dataAtIndexPath:indexPath], indexPath);
    }
}

#pragma mark - private
- (void)_configurationCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath{
    
    id dataAtIndex = [self.dataSource dataAtIndexPath:indexPath];
    
    if ([cell conformsToProtocol:@protocol(CellBinder)]) {
        id<CellBinder> binder = (id<CellBinder>)cell;
        [binder bindWithViewModel:dataAtIndex];
    }
    
    if (self.cellConfiguration) {
        self.cellConfiguration(cell,dataAtIndex, indexPath);
    }
}

- (NSString *)identifierForIndexPath:(NSIndexPath *)indexPath{
    NSString *identifier = self.identifier;
    if (self.identifierDelegate) {
        identifier = [self.identifierDelegate identifierForCellAtIndexPath:indexPath];
    }
    return identifier;
}

@end
